﻿namespace @(Model.Namespace).DAL
{
    /// <summary>
    /// @(Model.CnName)数据访问层
    ///</summary>
    public class @(Model.Name)DAL : BaseDAL, I@(Model.Name)DAL
    {
        /// <summary>
        /// 获取@(Model.CnName)分页结果
        ///</summary>
        public async Task<PageResult<Get@(Model.Name)PageResultOutput>> GetPageResultAsync(Get@(Model.Name)PageResultInput input)
        {
            PageResult<Get@(Model.Name)PageResultOutput> result = new ();
            RefAsync<int> totalNumber = 0;
            result.Datas = await DbContext.Queryable<@(Model.Name)>()
    @foreach (var classProperty in Model.ClassPropertys)
    {
        if (classProperty.PropertyType == "string")
        {<text>
                .WhereIF(!string.IsNullOrEmpty(input.@(classProperty.Name)), x => x.@(classProperty.Name).Contains(input.@(classProperty.Name)))</text>
    }
    else
    {
        <text>
                .Where(x => x.@(classProperty.Name) == input.@(classProperty.Name))</text>
    }
    }
    
                .Select<Get@(Model.Name)PageResultOutput>()            
                .ToPageListAsync(input.PageIndex, input.PageSize, totalNumber);
            result.DataCount = totalNumber.Value;
            return result;
        }

        /// <summary>
        /// 获取@(Model.CnName)分页列表
        ///</summary>
        public async Task<List<Get@(Model.Name)PageListOutput>> GetPageListAsync(Get@(Model.Name)PageListInput input)
        {
            return await DbContext.Queryable<@(Model.Name)>()
    @foreach (var classProperty in Model.ClassPropertys)
    {
        if (classProperty.PropertyType == "string")
        {<text>
                .WhereIF(!string.IsNullOrEmpty(input.@(classProperty.Name)), x => x.@(classProperty.Name).Contains(input.@(classProperty.Name)))</text>
    }
    else
    {
        <text>
                .Where(x => x.@(classProperty.Name) == input.@(classProperty.Name))</text>
    }
    }
    
                .Select<Get@(Model.Name)PageListOutput>()        
                .ToPageListAsync(input.PageIndex, input.PageSize);
        }

        /// <summary>
        /// 获取@(Model.CnName)
        /// </summary>
        @if (Model.IsGuidKey)
        {<text>
        public async Task<Get@(Model.Name)Output> GetAsync(BaseGuidEntity input)</text>}
            else
            {<text>
        public async Task<Get@(Model.Name)Output> GetAsync(BaseKeyEntity input)</text>}
        {
            return await DbContext.Queryable<@(Model.Name)>()
                .Where(x => x.Id == input.Id)
                .Select<Get@(Model.Name)Output>()        
                .FirstAsync();
        }

        /// <summary>
        /// 获取全部@(Model.CnName)
        /// </summary>
        public async Task<List<Get@(Model.Name)ItemOutput>> GetAllListAsync()
        {
            return await DbContext.Queryable<@(Model.Name)>()
                .Select<Get@(Model.Name)ItemOutput>()        
                .ToListAsync();
        }

        /// <summary>
        /// 修改@(Model.CnName)
        /// </summary>
        public async Task UpdateAsync(Update@(Model.Name)Input input)
        {
            @(Model.Name) entity = Mapper.Map<@(Model.Name)>(input);
            await DbContext.Updateable(entity)
                .ExecuteCommandAsync();
        }

        /// <summary>
        /// 新增@(Model.CnName)
        /// </summary>
        public async Task AddAsync(Add@(Model.Name)Input input)
        {
            @(Model.Name) entity = Mapper.Map<@(Model.Name)>(input);
    @if (Model.IsGuidKey)
    {<text>
            entity.Id = Guid.NewGuid().ToString();</text>
    }
            await DbContext.Insertable(entity).ExecuteCommandAsync();
        }

        /// <summary>
        /// 删除@(Model.CnName)
        /// </summary>
    @if (Model.IsGuidKey)
    {<text>
        public async Task DeleteAsync(BaseGuidListInput input)</text>}
            else
            { <text>
        public async Task DeleteAsync(BaseIdListInput input)</text>}
        {
            await DbContext.Deleteable<@(Model.Name)>()
                .Where(x => input.Ids.Contains(x.Id))
                .ExecuteCommandAsync();
        }
    }
}